﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Monkey.Framework.Logging;
using QuanLyHopDong.Biz.Model;

namespace QuanLyHopDong.Biz
{
    public class EmployeeBiz
    {
        #region Selection

        public Employee Logon(string userName, string password)
        {
            using (var context = new HopDongEntities())
            {
                return context.Employees.FirstOrDefault(e => e.UserName == userName && e.Password == password);
            }
        }

        public Employee GetById(int id)
        {
            if (id <= 0) return null;
            using (var context = new HopDongEntities())
            {
                return context.Employees.FirstOrDefault(x => x.Id == id);
            }
        }

        public List<Employee> GetEmployees(int startIndex, int pageSize, string sorting, string name, out int totalRecords)
        {
            using (var db = new HopDongEntities())
            {
                var query = db.Employees.Where(x => x.UserName != "admin");

                // filter
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(x => x.Name.Contains(name));
                }

                totalRecords = query.Count();

                return pageSize > 0
                           ? query.OrderBy(sorting).Skip(startIndex).Take(pageSize).ToList() //Paging
                           : query.ToList(); //No paging
            }
        }

        public Employee GetByUserName(string userName)
        {
            using (var db = new HopDongEntities())
            {
                return db.Employees.FirstOrDefault(x => x.UserName == userName);
            }
        }

        public List<Employee> GetAllEmployees()
        {
            using (var db = new HopDongEntities())
            {
                return db.Employees.Where(x => x.UserName != "admin").ToList();
            }
        } 

        #endregion

        #region CRUD

        public Employee CreateEmployee(Employee employee)
        {
            // validate inputs
            if (employee == null) return null;
            if (string.IsNullOrEmpty(employee.Name)) return null;

            // is existed username
            using (var db = new HopDongEntities())
            {
                var existedEmp = db.Employees.FirstOrDefault(x => x.UserName == employee.UserName);

                if (existedEmp != null)
                    throw new ApplicationException("UserNameIsExisted");
            }

            using (var db = new HopDongEntities())
            {
                db.Employees.Add(employee);
                db.SaveChanges();
            }

            return employee;
        }

        public void UpdateEmployee(Employee employee)
        {
            if (employee == null || employee.Id == 0) return;

            if (string.IsNullOrEmpty(employee.UserName) || string.IsNullOrEmpty(employee.Password))
            {
                throw new ApplicationException("UserNamePasswordRequired");
            }

            using (var context = new HopDongEntities())
            {
                var countExisted = context.Employees.Count(x => x.Id != employee.Id && x.UserName == employee.UserName);
                if (countExisted > 0)
                    throw new ApplicationException("UserNameIsExisted");

                context.Employees.Attach(employee);
                context.Entry(employee).State = EntityState.Modified;
                context.SaveChanges();
            }
        }

        public void DeleteEmployee(int employeeId)
        {
            if (employeeId <= 0) return;
            using (var context = new HopDongEntities())
            {
                var employee = context.Employees.First(g => g.Id == employeeId);

                context.Entry(employee).State = EntityState.Deleted;

                context.SaveChanges();
            }
        }

        #endregion
    }
}
